<template>
    <div>
        <div class="background">
            <el-carousel indicator-position="outside"  :interval="4000" height="950px">
                <el-carousel-item v-for="(p,i) in loginImg" :key="i">
                    <img :src="p.url" width="100%" height="100%">
                </el-carousel-item>
            </el-carousel>
            <div class="contain">
                <h1>注册新用户</h1>
                <div class="form">
                    <label>用户名：</label><input type="text" v-model.trim="name" placeholder="请输入用户名"><br/>
                    <div v-if="myshow" class="tishi">用户名不少于四个字符！</div>
                </div>
                <div class="form">
                    <label>密码：</label><input type="password" v-model.trim="password" placeholder="请输入密码"><br/>
                    <div v-if="myshow1" class="tishi">密码格式错误！</div>
                </div>
                <div class="form">
                    <label>确认密码：</label><input type="password" v-model.trim="repassword" placeholder="请再次输入密码"><br/>
                    <div v-if="myshow2" class="tishi">两次输入的密码不一致！</div>
                </div>
                <div class="form">
                    <label>邮箱：</label><input type="email" v-model.trim="mail" placeholder="请输入电子邮箱"><br/>
                    <div v-if="myshow3" class="tishi">请输入正确的电子邮箱！</div>
                </div>
                <div class="form">
                    <label>手机号：</label><input type="phone" v-model.trim="phone" placeholder="请输入手机号"><br/>
                    <div v-if="myshow4" class="tishi">手机号格式错误！</div>
                </div>
                <div class="form" style="position:relative">
                    <input type="checkbox" style="width:20px;position:absolute;bottom:0;left:-40px;"><span>我已阅读并同意目前的用户<span style="font-weight:600">隐私协议</span> </span> <br/>
                </div>
                <button @click.prevent="handlefinish" @click="userregi">提交</button>
            </div>
        </div>
        <my_footer/>
    </div>
    
</template>

<script>
export default {
    name:'register',
    props: {
    msg: String
  },
  data(){
    return{
      myshow:false,
			myshow1:false,
      myshow2:false,
			myshow3:false,
      myshow4:false,
      name:"",
      password:"",
      repassword:"",
      mail:"",
      phone:"",
      loginImg:[
             {id:1,url:require("../assets/imgs/xm002-f.jpg")},
             {id:2,url:require("../assets/imgs/xm002-f.jpg")},
             {id:3,url:require("../assets/imgs/xm002-f.jpg")},
             {id:4,url:require("../assets/imgs/xm002-f.jpg")}
         ]
    };
  },
  methods:{
  //点击完成按钮触发handlefinish
    handlefinish:function()
    {
      if(localStorage['name']===this.name)
      {
        this.$message("用户名已存在");//如果用户名已存在则无法注册
      }
      else if(this.name==='')
      {
        this.$message("用户名不能为空");
      }else if(this.password===''){
          this.$message("密码不能为空");
      }else if(this.repassword===''){
          this.$message("密码不能为空");
      }else if(this.mail===''){
          this.$message("电子邮箱不能为空");
      }else if(this.phone===''){
          this.$message("联系电话不能为空");
      }
      else{//将新用户信息存储到localStorage
        localStorage.setItem('name',this.name);
        localStorage.setItem('password',this.password);
        localStorage.setItem('repassword',this.repassword);
        localStorage.setItem('mail',this.mail);
        localStorage.setItem('phone',this.phone);
        localStorage.setItem('s',"false");
        this.$message("注册成功");
        this.$router.replace('/login');//完成注册后跳转至登录页面
      }
    },
    //注册函数 点击触发
    userregi(){
        //点击注册后发送一个请求
      this.axios
      //传手机号
        .get("/phonerepeat",{params:{phone:this.phone}
        }).then(result=>{
            //判断手机号 是否被注册 如果被注册
            //服务器端 返回 201
          if (result.data.code == 201) {
            alert("手机号已注册");
          }else{
            //发送一个axios请求 传值
            this.axios
              .post(
                "/username",
                //传V-moudel的值
                `phone=${this.phone}&uname=${this.name}&password=${this.password}&email=${this.mail}`
                //接受返回值
              ).then((result) => {
                if (result.data.code === 200) {
                  this.islogindetale();
                  alert("注册成功");
                  this.$router.replace('/Login');//完成注册后跳转至登录页面
                }
              });
          }
        });
      console.log("执行了");
    },
    //清空v-model中的内容
    islogindetale() {
      (this.phone = ""), (this.uname = ""), (this.parssword = "");
    }
  },
  watch:{
    
    name(){
      if(this.name.length<4){
        this.myshow=true
      }else{
        this.myshow=false
      }
      if(this.phone==""){
        this.myshow=fasle
      }
    },
		password(){
			//强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在 8-10 之间)
			let reg1 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/;
			if(!reg1.test(this.password)){
				this.myshow1=true
			}else if(reg1.test(this.password)){
				this.myshow1=false
			}
			else{
				this.myshow1=false
				
			}
      this.myshow1=this.password==''?false:true;
		},
    repassword(){
      if(this.repassword!==this.password){
        this.myshow2=true
      }else{
        this.myshow2=false
      }
      this.myshow2=this.repassword==''?false:true;
    },
    mail(){
      let reg3 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
			if(!reg3.test(this.mail)){
				this.myshow3=true
			}else if(reg3.test(this.mail)){
				this.myshow3=false
			}
			else{
				this.myshow3=false
			}
			this.myshow3=this.mail==''?false:true;
    },
    phone(){
			let reg4 = /^[1][3,4,5,7,8][0-9]{9}$/
			if(!reg4.test(this.phone)){
				this.myshow4=true
			}else if(reg4.test(this.phone)){
				this.myshow4=false
			}
			else{
				this.myshow4=false
			}
			this.myshow4=this.phone==''?false:true;
		}
  }
};
</script>

//css
<style scoped lang="scss">
  .el-carousel__item img {
            height:994px;
            width:100%;
        }
        .el-carousel__item:nth-child(2n) {
            background-color: #99a9bf;
        }
        .el-carousel__item:nth-child(2n+1) {
            background-color: #d3dce6;
        }
.background{
    height: 1000px;
    width:100%;
    /* height: 880px; */
    /* background: url("../../public/home/banner1.jpg"); */
    background-size:100% 100%;
    position: relative;
    top: 0;
    left: 0; 
}
.contain{
  z-index: 100;
  width: 600px;
  height: 650px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#00000090;
  text-align: center;
  border-radius: 20px;
  
}
.contain h1{
  color: white;
  margin-top: 50px;
}
.form{
  color: white;
  margin-left: 20%;
  margin-top:40px;
  font-size: 20px;
  text-align: left;
  position: relative;
  .tishi {
    font-family: PingFangSC-Regular;
    font-size: 13px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0;
    color: red;
    position: absolute;
    left: 150px;
  }
}

label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: right;
}
input,textarea{
  margin-left: 10px;
  padding: 15px;
  border: solid 1px #ebd404;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 250px;
  height: 30px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
  border-radius: 10px;
}
input:hover,textarea:hover,input:focus,textarea:focus{border-color:#0d0aa1;}
button{
  position: relative;
  height: 33px;
  width: 150px;
  background: rgba(115, 108, 216, 0.425);
  border-radius: 10px;
  margin-top: 50px;
  box-shadow: none;
  color: white;
  margin-left: 0px;
}
</style>
